<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #div1 {
        position: absolute;
        left: 1000px;
        width: 100px;
        height: 100px;
        top: 50px;
        background-color: hotpink;
      }
      span {
        position: absolute;
        width: 1px;
        height: 100%;
        background-color: black;
        left: 500px;
      }
    </style>
    <script>
      window.onload = function () {
        var oBtn = document.getElementById("btn");
        var oDiv = document.getElementById("div1");
        oBtn.onclick = function () {
          startMove(500);
        };
        var timer = null;
        function startMove(oTarget) {
          clearTimeout(timer);
          timer = setInterval(function () {
            // var speed = Math.ceil((oTarget - oDiv.offsetLeft) / 8);
            var speed = (oTarget - oDiv.offsetLeft) / 8;
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
            if (oDiv.offsetLeft == oTarget) {
              clearTimeout(timer);
            }
            oDiv.style.left = oDiv.offsetLeft + speed + "px";
          }, 30);
        }
      };
    </script>
  </head>
  <body>
    <button id="btn">开始运动</button>
    <div id="div1"></div>
    <span></span>
  </body>
</html>
